/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.directedit.ILabelControler;
import com.cea.papyrus.core.directedit.LabelCellEditorLocator;
import com.cea.papyrus.core.directedit.LabelControler;
import com.cea.papyrus.core.directedit.LabelDirectEditManager;
import com.cea.papyrus.core.directedit.PropertyAccessor;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.ContainerHighlightEditPolicy;
import com.cea.papyrus.core.editpart.ContainerWithSemanticEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.composite.figure.CollaborationFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller for a {@link Collaboration}.
 */
public class CollaborationEditPart extends ContainerWithSemanticEditPart {

	/**
	 * Manager for editing collaboration name.
	 */
	private DirectEditManager manager;
	
	/**
	 * Creates a new CollaborationEditPart.
	 * 
	 * @param modelManager 
	 * @param graphNode 
	 */
	public CollaborationEditPart(GraphNode graphNode, ModelManager modelManager){
		super(graphNode,modelManager);
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.ContainerWithSemanticEditPart#createEditPolicies()
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
        // overload the GRAPHICAL_NODE_ROLE editPolicy to use the diagram specific one
        installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphNodeEditPolicy() );
        // other editPolicies
		installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE,
				new ContainerHighlightEditPolicy());
		if(getContentPane() != null) {
			installEditPolicy(EditPolicy.LAYOUT_ROLE, new CompositeLayoutEditPolicy(
				(XYLayout) getContentPane().getLayoutManager()));
		}
	}
	
	/**
	 * 
	 * 
	 * @param request 
	 */
	public void showTargetFeedback(Request request){}
	
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		manager = null;	
		//  Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		if (fig != null) {
			return fig;
		}
		// else
		return new CollaborationFigure();
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public org.eclipse.uml2.uml.Collaboration getUmlElement() {
		Element e=((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		// case of element import
		if(e instanceof ElementImport){
			return (org.eclipse.uml2.uml.Collaboration)((ElementImport) e).getImportedElement();
		}
		// this is a simple Collaboration
		return (org.eclipse.uml2.uml.Collaboration)e;
	}
	

	/**
	 * 
	 */
	protected void refreshVisuals() {
		Debug.debug(this, "refreshvisuals", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		// if fig is a stereotype figure
		if (!(fig instanceof CollaborationFigure)) {
			IconFigure icf= (IconFigure)getFigure();
			icf.setName(((NamedElement)getUmlElement()).getName());

		} else {
			// Retrieve current class and figure
			org.eclipse.uml2.uml.Collaboration currentCollaboration = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());

			CollaborationFigure f = (CollaborationFigure) fig; //get Class figure
			// if fig is a ClassFigure
			((NodeFigure) fig).refreshColors(getGraphNode());
			((NodeFigure) fig).refreshPresentation(getPresentation(), currentCollaboration, getGraphNode());
			boolean hasIcon  = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();
			
			//f.refreshStereotypes(currentElement.getStereotypes(), getPresentation(), hasIcon, hasShape);
			f.refreshStereotypes(f.stereotypesLabel(this), getPresentation(), hasIcon, hasShape);
		
			f.setName(currentCollaboration.getName());// set the name of the class

			// refresh qualifiedName
			GraphNode gp= (GraphNode)getGraphNode().getContainer();
			f.refreshQualifiedName(gp, getGraphNode(), getUmlElement());
			//setAbstract
			
		}

		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				fig,
				r);

	}

	/**
	 * Returns the Figure of this as a PackageFigure.
	 * 
	 * @return PackageFigure of this.
	 */
	protected CollaborationFigure getPackageBoardFigure() {
		return (CollaborationFigure) getFigure();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getContentPane()
	 */
	/**
	 * Gets the content pane.
	 * 
	 * @return the content pane
	 */
	@Override
	public IFigure getContentPane() {
		IFigure fig = getFigure();
		if(fig instanceof CollaborationFigure) {
			return ((CollaborationFigure)fig).getContentsPane();
		}
		return null;
	}

	/**
	 * Returns the Name Label of this edit part.
	 * 
	 * @return Label
	 */
	protected Label getNameLabel() {
		IFigure fig = getFigure();

		if (fig instanceof IconFigure) {
			return (Label) fig;
		} else if (fig instanceof CollaborationFigure) {
			return ((CollaborationFigure) fig).getNameLabel();
		} else {
			Debug.error(this, "CollaborationEditPart::getNameLabel() : Unknown Figure type.", channel);
			return null;
		}
	}

	/**
	 * the direct edit mechanism requires the edit part to handle the direct
	 * edit request. This method determines which label the user clicked on. The
	 * label is passed to the ConditionessionCellEditorLocator which positions
	 * the cell editor over the selected label.
	 * 
	 * @param req the req
	 */
	public void performDirectEdit(Request req) {
		initLabelEditManager();
		manager.show();
	}

	/**
	 * Init the label direct edit manager.
	 * This method should be called when a Direct Edit request is performed.
	 */
	protected void initLabelEditManager() {
		if(manager==null) {
			// locator for the cell editor (on the name label)
			CellEditorLocator locator = new LabelCellEditorLocator(getNameLabel());

			// defining how properties are modified in the model
			PropertyAccessor accessor = new PropertyAccessor() {
				public String getPropertyName() {
					return "Name";
				}
				
				public String getValue() {
					return getUmlElement().getName();
				}
				public void setValue(String value) {
					getUmlElement().setName(value);  
				}
			};
			// defines the controler: link between model and view
			ILabelControler labelControler = new LabelControler(getNameLabel(), accessor);

			// creates the editing manager.
			manager = new LabelDirectEditManager(this, locator, labelControler);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 */
	@Override
	public void performRequest(Request request){
		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType())) {
			performDirectEdit(request);
		}
	}
	
}
